# Maintainer: Martell Malone <martellmalone@gmail.com>

_realname=glfw
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=3.2.1
pkgrel=2
pkgdesc="A free, open source, portable framework for OpenGL application development (mingw-w64)"
arch=('any')
url="http://www.glfw.org/"
license=('custom:ZLIB')
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs")
makedepends=("${MINGW_PACKAGE_PREFIX}-gcc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-vulkan-headers")
options=('staticlibs' 'strip')
source=(https://downloads.sourceforge.net/project/glfw/glfw/${pkgver}/${_realname}-${pkgver}.tar.gz
        001-3.2-cmake-suffix.patch
        002-3.2-vulkan_test.patch
        003-3.2-cmake-install.patch)
sha256sums=('8e3252932fb15134aded8780c8c5cc89da0a9007e139ca6d9ab0ed7df6d3f9a0'
            '3ee710c0628e4fa5b46099bcd90c9bc1b40dcf937611428a97a02e402cbfafca'
            'fb03a7c00bdb80d56945264c695e6fdefeda5fe95b2b0537364d97904ae5eeb0'
            'a15b6d2b5550d404b107eeb12ad15abe83fb340df70fd57939c9439e26b4d578')

prepare() {
  cd "${srcdir}/glfw-${pkgver}"
  patch -Np1 -i ${srcdir}/001-3.2-cmake-suffix.patch
  patch -Np1 -i ${srcdir}/002-3.2-vulkan_test.patch
  patch -Np1 -i ${srcdir}/003-3.2-cmake-install.patch
}

build() {
  # Build shared libs
  [[ -d ${srcdir}/shared-${MINGW_CHOST} ]] && rm -rf ${srcdir}/shared-${MINGW_CHOST}
  mkdir -p ${srcdir}/shared-${MINGW_CHOST}
  cd ${srcdir}/shared-${MINGW_CHOST}

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake \
    -G"MSYS Makefiles" \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    -DCMAKE_C_COMPILER=${MINGW_CHOST}-gcc \
    -DBUILD_SHARED_LIBS=ON \
    -DGLFW_BUILD_DOCS=OFF \
    ../${_realname}-${pkgver}
  make

  # Build static libs
  [[ -d ${srcdir}/static-${MINGW_CHOST} ]] && rm -rf ${srcdir}/static-${MINGW_CHOST}
  mkdir -p ${srcdir}/static-${MINGW_CHOST}
  cd ${srcdir}/static-${MINGW_CHOST}

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake \
    -G"MSYS Makefiles" \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    -DCMAKE_C_COMPILER=${MINGW_CHOST}-gcc \
    -DBUILD_SHARED_LIBS=OFF \
    -DGLFW_BUILD_DOCS=OFF \
    ../${_realname}-${pkgver}
  make
}

package() {
  cd "${srcdir}/static-${MINGW_CHOST}"
  make DESTDIR=${pkgdir} install

  cd "${srcdir}/shared-${MINGW_CHOST}"
  make DESTDIR=${pkgdir} install

  #copy license file
  mkdir -p "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}"
  cp "${srcdir}/${_realname}-${pkgver}/COPYING.txt" "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}"
}
